﻿/* Copyright 2011 Teun Duynstee
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using MongoDB.Bson;

namespace MongoAspnetProviders
{
    public interface IStoreWrapper<T>
    {
        void Update(IMongoQuery where, IMongoUpdate update, UpdateFlags flags);
        T FindOne(IMongoQuery searchMe);
        IList<T> Find(IMongoQuery where);
        IList<T> Find(IMongoQuery where, int from, int limit, out int total);
        SafeModeResult Remove(IMongoQuery where);
        void Save(T obj);
    }
    internal class DefaultStoreWrapper<T> : IStoreWrapper<T>
    {
        private MongoCollection<T> _innerCollection;
        public DefaultStoreWrapper(MongoCollection<T> innerCollection)
        {
            _innerCollection = innerCollection;
        }

        #region IStoreWrapper<T> Members
        public SafeModeResult Remove(IMongoQuery where)
        {
            return _innerCollection.Remove(where);
        }

        public void Update(IMongoQuery where, IMongoUpdate update, UpdateFlags flags)
        {
            _innerCollection.Update(where, update, flags);
        }

        public T FindOne(IMongoQuery searchMe)
        {
            return _innerCollection.FindOne(searchMe);
        }
        public IList<T> Find(IMongoQuery where)
        {
            int total;
            return Find(where, 0, int.MaxValue, out total);
        }
        public IList<T> Find(IMongoQuery where, int from, int limit, out int total)
        {
            MongoCursor<T> cursor = _innerCollection.FindAs<T>(where);
            List<T> result = new List<T>();
            total = cursor.Count();
            cursor.Skip = from;
            cursor.Limit = limit;
            foreach (var item in cursor)
            {
                result.Add(item);
            }
            if (from == 0 && limit > result.Count)
            {
                total = result.Count;
            }
            else
            {
                total = FindCount(where);
            }
            return result;
        }
        private IList<T> Find(IMongoQuery where, int from, int limit)
        {
            MongoCursor<T> cursor = _innerCollection.FindAs<T>(where);
            List<T> result = new List<T>();
            cursor.Skip = from;
            cursor.Limit = limit;
            foreach (var item in cursor)
            {
                result.Add(item);
            }
            return result;
        }
        private int FindCount(IMongoQuery where)
        {
            MongoCursor<T> cursor = _innerCollection.FindAs<T>(where);
            return cursor.Count();
        }

        public void Save(T obj)
        {
            _innerCollection.Save(obj);
        }

        #endregion
    }
}
